package First;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.mysql.cj.jdbc.Driver;

import First.Window;

public class Add extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;

    JLabel jlnumber = new JLabel("学号：");
    JLabel jlname = new JLabel("姓名：");
    JLabel jlsex = new JLabel("性别：");
    JLabel jlbirthday = new JLabel("出生日期：");
    JLabel jlprofession = new JLabel("专业：");
    JLabel jlCno = new JLabel("课程号");
    JLabel jlGrade = new JLabel("成绩");

    JTextField jtnumber = new JTextField("",20);
    JTextField jtname = new JTextField("",20);
    JTextField jtsex = new JTextField("",20);
    JTextField jtbirthday = new JTextField("",20);
    JTextField jtprofession = new JTextField("",20);
    JTextField jtCno = new JTextField("",20);
    JTextField jtGrade = new JTextField("",20);

    JButton buttonadd = new JButton("添加");
    JButton buttonreturn = new JButton("返回");

    public Add() {
        JPanel jpnumber = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpbirthday = new JPanel();
        JPanel jpprofession = new JPanel();
        JPanel jpCno = new JPanel();//课程号
        JPanel jpGrade = new JPanel();//成绩
        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jpnumber.add(jlnumber);
        jpnumber.add(jtnumber);

        jpname.add(jlname);
        jpname.add(jtname);

        jpsex.add(jlsex);
        jpsex.add(jtsex);

        jpbirthday.add(jlbirthday);
        jpbirthday.add(jtbirthday);

        jpprofession.add(jlprofession);
        jpprofession.add(jtprofession);

        jpCno.add(jlCno);
        jpCno.add(jtCno);

        jpGrade.add(jlGrade);
        jpGrade.add(jtGrade);

        jpforbutton.add(buttonadd);
        jpforbutton.add(buttonreturn);

        buttonadd.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                //Add
                Connection conn = null;
                Statement stat = null;
                PreparedStatement ps1=null;
                PreparedStatement ps2=null;
                //String sql = "INSERT INTO Student(Sno,Sname,Ssex,Sbirthday,Sprofession) values(?,?,?,?,?)"+"insert into SC (Sno,Cno,Grade) values (?,?,?)";
                String sql1 = "INSERT INTO Student(Sno,Sname,Ssex,Sbirthday,Sprofession) values(?,?,?,?,?)";
                String sql2 = "insert into SC (Sno,Cno,Grade) values (?,?,?)";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("JBDC 加载成功!");
                }catch(Exception a){
                    System.out.println("JBDC 失败!");
                    a.printStackTrace();
                }
                try{
                    conn=DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT%2B8",
                            "root",
                            "12345678");
                    ps1=conn.prepareStatement(sql1);
                    ps1.setString(1,jtnumber.getText());
                    ps1.setString(2,jtname.getText());
                    ps1.setString(3,jtsex.getText());
                    ps1.setString(4,jtbirthday.getText());
                    ps1.setString(5,jtprofession.getText());
                    ps1.executeUpdate();
                    ps2=conn.prepareStatement(sql2);
                    ps2.setString(1,jtnumber.getText());
                    ps2.setString(2,jtCno.getText());
                    ps2.setString(3,jtGrade.getText());
                    ps2.executeUpdate();



                }catch (SQLException b){
                    b.printStackTrace();
                }finally{
                    try{
                        JOptionPane.showMessageDialog(null, "添加成功！", "提示",JOptionPane.PLAIN_MESSAGE);
                        conn.close();
                        System.out.println("MySQL 关闭成功");
                    }catch (SQLException c){
                        System.out.println("MySQL 关闭失败 ");
                        c.printStackTrace();
                    }
                }
            }}
        );
        buttonreturn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dispose();
            }
        });
        this.setTitle("添加学生信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirthday);
        this.add(jpprofession);
        this.add(jpCno);
        this.add(jpGrade);
        this.add(jpforbutton);
        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}